#include "ColorButton.h"
#include <QPainter>

//! @class ColorButton
ColorButton::ColorButton(QWidget* parent)
    : QToolButton(parent)
{
    setColor(Qt::black);
}

QColor ColorButton::color() {
    return _color;
}

void ColorButton::setColor(QColor value) {
    if (_color != value) {
        _color = value;
        // fill icon with _color
        QPixmap pixmap(iconSize());
        pixmap.fill(_color);
        // decorate the icon with border rect
        QPainter painter(&pixmap);
        painter.setPen(Qt::darkMagenta);
        painter.drawRect(0, 0, iconSize().width()-1, iconSize().height()-1);
        // set icon to the button
        setIcon(QIcon(pixmap));
    }
}

/*!
 * @reimp
 * Sets button style to show text beside icon on text set for conveniency.
 */
void ColorButton::setText(const QString &text) {
    QToolButton::setText(text);
    setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
}
